import pandas as pd
import folium
import geojson
from matplotlib import pyplot as plt
import numpy as np
from PIL import Image
import os
import time
from selenium import webdriver
from colour import Color
Used libraries:
- folium
- geojson
- PIL
- os
- selenium
- colour
To crop images chromedriver.exe must be placed in the executable directory
grouped=pd.read_csv('grouped.csv') #.drop(columns=['trip_duration', 'dropoff_datetime'])
grouped.head()
regions=pd.read_csv('regions.csv', sep=(';'))
regions.head()
# The full number of cells is
regions.shape[0]
# Calculate the number of cells with no one trip
number_of_zero_trips=regions.shape[0]-grouped.shape[1]
print('The number of of cells with no one trip is '+str(number_of_zero_trips))
def generateBaseMap(default_location=[ 40.748817, -73.985428], default_zoom_start=11):
base_map = folium.Map(location=default_location, tiles='cartodbpositron',
width=900,
height=880, control_scale=True,
zoom_start=default_zoom_start,
no_touch=True,zoom_control=True )
return base_map
def data2geojson(df):
features = []
insert_features = lambda X: features.append(
geojson.Feature(geometry = geojson.Polygon([[(X['west'], X['south']),
(X['east'], X['south']),
(X['east'], X['north']),
(X['west'], X['north'])]]),
id = str(X.name),
properties=dict(name=X["region"],
description=X["region"])))
df.apply(insert_features, axis = 1)
feature_collection = geojson.FeatureCollection(features)
dumps = geojson.dumps(feature_collection)
return geojson.loads(dumps)
geo_data = data2geojson(regions)
base_map = generateBaseMap()
# set grid to map
folium.Choropleth(geo_data = geo_data,
fill_color = '#00000000',
line_opacity=0.2).add_to(base_map)
# set marker of Empire State Building
folium.Marker([40.748817, -73.985428]).add_to(base_map)
# set text Empire State Building
folium.map.Marker(
[40.748817, -73.985428],
icon=folium.features.DivIcon(
icon_size=(200,36),
icon_anchor=(0,0),
html='<div style="font-size: 9pt; color: Maroon"><b>%s</b></div>' % 'Empire State Building',
)
).add_to(base_map)
# visualize map
base_map
def save_map(geomap):
# Save map to html
geomap.save('map.html')
# Convert to png image format
delay=5
fn='map.html'
tmpurl='file://{path}/{mapfile}'.format(path=os.getcwd(),mapfile=fn)
base_map.save(fn)
browser = webdriver.Chrome()
browser.get(tmpurl)
time.sleep(delay)
browser.save_screenshot('map.png')
# Crop image and save
im = Image.open('map.png')
width, height = im.size
left = 54
top = 116
right = width-left-6
bottom = height
im = im.crop((left, top, right, bottom))
im.save('map_in_png.png')
browser.quit()
save_map(base_map)
# map visualization plot
def map_visualization(longitude, latitude,s=7):
x_min=-74.25559
x_max=-73.70001
y_min=40.49612
y_max=40.91553
img = plt.imread('map_in_png.png')
fig, ax = plt.subplots(figsize=(12, 12))
fig.set_facecolor('.96')
ax.imshow(img, extent = [x_min, x_max, y_min,y_max])
ax.scatter(x=longitude, y=latitude, c='g' , s=s, label='Taxi pickup point')
ax.legend(loc='upper left', fontsize=16)
plt.title('New York city map', size=20, y=1.03)
plt.xlabel('Longitude')
plt.ylabel('Latitude')
ax.xaxis.grid(True, which='major', linewidth=0.1,
color='black', linestyle='--')
ax.yaxis.grid(True, which='major', linewidth=0.1,
color='black', linestyle='--')
map_visualization(-73.985428, 40.748817)
long_mean=(regions.west+regions.east)/2
lat_mean=(regions.south+regions.north)/2
map_visualization(long_mean, lat_mean)
grouped_count=grouped.sum().to_frame().reset_index().rename(columns={'index': 'region', 0:'count'})
grouped_count['count']=grouped_count['count'].astype('int')
grouped_count
# create folium heatmap
base_map = generateBaseMap()
choropleth=folium.Choropleth(
geo_data = geo_data,
data=grouped_count,
columns=['region', 'count'],
key_on='feature.id',
fill_color='YlOrBr',
nan_fill_color='#00000000',
fill_opacity=0.6,
line_opacity=0.2,
smooth_factor=3,
legend_name='Taxi trips count',
reset=True).add_to(base_map)
# set marker of Empire State Building
#folium.Marker([40.748817, -73.985428]).add_to(base_map)
# add popup of regions to determine the number of region
style_function = "font-size: 15px; font-weight: bold"
choropleth.geojson.add_child(
folium.features.GeoJsonTooltip(['name'], style=style_function, labels=False))
# set text Empire State Building
folium.map.Marker(
[40.748817, -73.985428],
icon=folium.features.DivIcon(
icon_size=(200,36),
icon_anchor=(0,0),
html='<div style="font-size: 9pt; color: Maroon"><b>%s</b></div>' % 'Empire State Building',
)
).add_to(base_map)
folium.LayerControl().add_to(base_map)
base_map
save_map(base_map)
trips_data = np.array(grouped_count['count'])
coord_data = np.array(regions)
# create static heatmap
# define colors
color_1 = 'LightYellow'
color_2 = 'Brown'
color_1 = Color(color_1)
grad = list(color_1.range_to(Color(color_2), int(trips_data.max())))
# Heatmap
fig, ax = plt.subplots(figsize=(12, 1.5))
fig.set_facecolor('.96')
trips_values = list(range(0, int(trips_data.max())+1, 500))
colorbar = np.array([[tuple([int(round(j*255, 0)) for j in grad[int(i)-1].rgb]) for i in trips_values] for _ in range(100)])
ax.imshow(colorbar)
plt.yticks([])
ticks = list(range(len(trips_values)))
plt.xticks(ticks=ticks[::100] + [ticks[-1]], labels=trips_values[::100] + [trips_values[-1]])
plt.title('Taxi trip counts heatmap', y=1.01, fontsize=20)
plt.show()
map_visualization(-73.985428, 40.748817, 70)
grouped_mean=grouped.mean().to_frame().reset_index().rename(columns={'index': 'region', 0:'mean'})
grouped_mean['mean']=grouped_mean['mean'].astype('int')
grouped_mean=grouped_mean[grouped_mean['mean']>=5]
grouped_mean
# create folium heatmap
base_map = generateBaseMap()
choropleth=folium.Choropleth(
geo_data = geo_data,
data=grouped_mean,
columns=['region', 'mean'],
key_on='feature.id',
fill_color='YlOrBr',
nan_fill_color='#00000000',
fill_opacity=0.6,
line_opacity=0.2,
smooth_factor=3,
legend_name='Taxi trips count',
reset=True).add_to(base_map)
#set marker of The Statue of Liberty
folium.Marker([40.689247, -74.044502]).add_to(base_map)
# add popup of regions to determine the number of region
style_function = "font-size: 15px; font-weight: bold"
choropleth.geojson.add_child(
folium.features.GeoJsonTooltip(['name'], style=style_function, labels=False))
folium.TileLayer('openstreetmap').add_to(base_map)
# set text Empire State Building
folium.map.Marker(
[40.689247, -74.044502],
icon=folium.features.DivIcon(
icon_size=(200,36),
icon_anchor=(0,0),
html='<div style="font-size: 9pt; color: Maroon"><b>%s</b></div>' % 'The Statue of Liberty',
)
).add_to(base_map)
folium.LayerControl().add_to(base_map)
base_map
print('The number of regions after filtering is '+str(len(grouped_mean)))
grouped_mean.region.unique()
len(grouped_mean.region)